package com.jinhongone.pattern.structure.combination;

import java.util.ArrayList;
import java.util.List;

/*
 * 具体菜单类：树枝节点
 *   作用：一个容器对象里面还可以包含子菜单
 * */
public class Menu extends MenuComponent {

    //所具有的子菜单
    private List<MenuComponent> menuComponents = new ArrayList<MenuComponent>();

    public Menu(String name, int level) {
        this.name = name;
        this.level = level;
    }

    @Override
    public void addSub(MenuComponent component) {
        menuComponents.add(component);
    }

    @Override
    public void removeSub(MenuComponent component) {
        menuComponents.remove(component);
    }

    @Override
    public MenuComponent getChild(int index) {
        return menuComponents.get(index);
    }

    @Override
    void print() {
        //添加菜单level缩进
        for (int i = 1; i < level ; i++) {
            //每差一级多输出一个【-】
            System.out.print("-");
        }
        //打印本菜单名称
        System.out.println(getName());
        //打印子菜单名称
        for (MenuComponent menuComponent : menuComponents) {
            menuComponent.print();
        }
    }
}
